<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>产品管理</el-breadcrumb-item>
      <el-breadcrumb-item>产品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片试图区域 -->
    <el-card>
      <div slot="header">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-input placeholder="请输入文字" v-model="queryInfo.query" clearable @clear="getGoodsList">
              <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
            </el-input>
          </el-col>
          <el-col :span="4">
            <el-button type="primary" @click="goAddPage">添加按钮</el-button>
          </el-col>
        </el-row>
      </div>
      <!-- card body -->
      <div>
        <el-table :data="goodslist" border stripe>
          <el-table-column type="index"></el-table-column>
          <el-table-column prop="goods_name" label="商品名称"></el-table-column>
          <el-table-column prop="goods_price" label="商品价格（元）" width="95px"></el-table-column>
          <el-table-column prop="goods_weight" label="商品重量" width="70px"></el-table-column>
          <el-table-column label="添加时间" width="140px">
            <template slot-scope="scope">{{scope.row.add_time|dateFormat}}</template>
          </el-table-column>
          <el-table-column label="操作" width="130px">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="removeById(scope.row.goods_id)"
              ></el-button>
              <!-- <span>{{ scope.row }}</span> -->
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="sizeChange"
          @current-change="currentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          background
          class="pagena"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      goodslist: [],
      total: 0
    }
  },
  methods: {
    // 根据分页获取对应商品列表
    getGoodsList: async function() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表信息失败')
      } else {
        this.goodslist = res.data.goods
        this.total = res.data.total
        this.queryInfo.pagenum = new Number(res.data.pagenum)

        console.log(res)
      }
    },
    sizeChange(newsize) {
      this.queryInfo.pagesize = newsize
      this.getGoodsList()
    },
    currentChange(newpage) {
      this.queryInfo.pagenum = newpage
      this.getGoodsList()
    },
    search() {
      this.getGoodsList()
    },
    async removeById(id) {
      const confirmResult = await this.$confirm('确定删除该商品吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult == 'confirm') {
        // this.$message.success('确认了本次删除操作'+id);
        const { data: res } = await this.$http.delete('goods/' + id)
        if (res.meta.status !== 200) {
          this.$message.error('删除失败！')
        } else {
          this.$message.success('删除成功！')
          this.getGoodsList()
        }
      } else {
        return this.$message.info('取消本次删除操作')
      }
    },
    goAddPage() {
      this.$router.push('/goods/add')
    }
  },
  created() {
    this.getGoodsList()
  }
}
</script>

<style >
.pagena {
  margin-top: 10px;
}
</style>
